// pages/storeShop/storeShop.js
var app = getApp();
var util = require('../../utils/util');
Page({

    /**
     * 页面的初始数据
     */
    data: {
        order_type:1,
        curPage:1,
        tishi_title: '正在加载中...',
    },

    /**
     * 生命周期函数--监听页面加载
     */
    onLoad: function (options) {
        var that = this;
        app.checkLogin(function(){
            that.getStoreOrder();
        })
    },
	onMyEventTap:function(e,id){
		console.log(e.detail);
		var that=this;
		util.showLoading();
			util.fetchPost("/api/store/order/order_back", {
				order_id: e.detail
			}, function (res) {
				if (res.state == 1) {
					util.hideLoading();
					util.showSuccess('操作成功');
					that.setData({
						curPage: 1,
						order_list:[]
					})
					that.getStoreOrder();
				} else {
					util.message(res.datas)
				}
			})
	},
    getStoreOrder:function(){
        var that = this;
        util.showLoading();
        var apiUrl = '/api/store/order/store_order';
        var apiData = {
        	order_type:that.data.order_type,
        	page:that.data.curPage,
        }
        util.fetchPost(apiUrl,apiData,function(res){
            util.hideLoading();
            if(res.state == 1){
                that.setData({
                    total:res.datas.total
                });
                // utils.hideLoading();
                var allpost = that.data.order_list;
                if (allpost == undefined) {
                    allpost = [];
                }
                for (var i in res.datas.list) {
                    allpost.push(res.datas.list[i])
                }
                if (res.datas.list.length < 10) {
                    that.setData({
                        tishi_title: '已加载全部',
                    });
                } else {
                    that.setData({
                        tishi_title: '正在加载中......',
                    });
                }
                that.setData({
                    order_list: allpost,
                    ready: res.datas.list.length == 0 ? 1 : 2,
                })
                console.log(that.data.order_list);
            }else{
                util.message(res.datas);
            }
        });
    },
    //tab切换
    changeType: function (e) {
        var that = this;
        that.setData({
        	curPage:1,
            order_type: that.data.order_type == 1 ? 2 : 1,
            order_list:[],
        })
        that.getStoreOrder();
    },
    detailedTap:function(){
		var that = this;
    	wx.navigateTo({
			url: '/pages/statistical/statistical?order_type=' + that.data.order_type
    	});
    },
    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady: function () {

    },

    /**
     * 生命周期函数--监听页面显示
     */
    onShow: function () {

    },

    /**
     * 生命周期函数--监听页面隐藏
     */
    onHide: function () {

    },

    /**
     * 生命周期函数--监听页面卸载
     */
    onUnload: function () {

    },

    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    onPullDownRefresh: function () {

    },

    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom: function () {
        var that = this;
        if(that.data.ready == 2){
            var page = that.data.curPage + 1
            that.setData({
                curPage: page,
            })
            that.getStoreOrder();
        }
    },

    /**
     * 用户点击右上角分享
     */
    onShareAppMessage: function () {

    }
})